package com.cw.frame.codegen.parameter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.cw.jeeyt.common.enums.EnumParamType;
import com.cw.lang.common.utils.DateUtil;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.frame.codegen.Configuration;

/**
 * 从页面获取数据封装
 *
 * @author chenw
 */
public class ParamContextFactory4PageImpl extends AbstractParamContextFactoryImpl {
    private static String enumFlag = "Enum";
    @Override
    protected void fulfillContextOnDemand(ParamContext context, Configuration configuration) throws Exception {
        // 获取包名
        String packageNames = configuration.getPackages();
        // 获取类名
        String entityClassNames = configuration.getEntityClasses();
        String tableCnNames = configuration.getTableCnNames();
        String tableNames = configuration.getTableNames();
        //主键id数据类型
        String pkIdDataTypes = configuration.getPkIdDataTypes();
        if (StringUtils.isEmpty(packageNames) || StringUtils.isEmpty(entityClassNames)) {
            return;
        }

        String[] packageNameArray = packageNames.split(",");
        int count = packageNameArray.length;
        String[] entityNamesArray = entityClassNames.split(",");
        String[] tableCnNameArray = tableCnNames.split(",");
        String[] tableNameArray = tableNames.split(",");
        if (entityNamesArray.length != count) {
            logger.error("要生成的表个数和包个数不对应");
            throw new Exception("要生成的表个数和包个数不对应");
        }
        //主键信息
        List<List<PkField>> pkLists = configuration.getPkList();
        //枚举信息
        List<EnumField> enumLists = configuration.getEnumLists();
        //表模型字段信息
        List<List<Field>> fieldLists = configuration.getFieldList();
        //查询字段信息
        List<List<Field>> queryLists = configuration.getQueryList();
        //排序字段信息
        List<List<Field>> orderLists = configuration.getOrderList();
        //列表字段信息
        List<List<Field>> listLists = configuration.getListList();
        //表单字段信息
        List<List<Field>> formLists = configuration.getFormList();

        List<ParamBundle> paramBundles = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            ParamBundle paramBundle = new ParamBundle();
            paramBundle.setClassName(entityNamesArray[i]);
            paramBundle.setPackageName(packageNameArray[i]);
            paramBundle.setFieldList(fieldLists.get(i));
            paramBundle.setQueryList(queryLists.get(i));
            paramBundle.setOrderList(orderLists.get(i));
            paramBundle.setFormList(formLists.get(i));
            paramBundle.setListList(listLists.get(i));
            paramBundle.setPkList(pkLists.get(i));
            String moduleName = paramBundle.getClassName()
                .substring(0, 1)
                .toLowerCase() + paramBundle.getClassName()
                .substring(1);
            paramBundle.setModuleName(moduleName);
            paramBundle.setModuleId(configuration.getModuleId());
            paramBundle.setModuleIdUp(configuration.getModuleId()
                .toUpperCase());
            paramBundle.setProjectCode(configuration.getProjectCode());
            paramBundle.setProjectType(configuration.getProjectType());
            paramBundle.setModuleIdCn(configuration.getModuleIdCn());
            paramBundle.setTableName(tableNameArray[i]);
            paramBundle.setTableCnName(tableCnNameArray[i]);
            paramBundle.setAuthor(configuration.getAuthor());
            paramBundle.setSerialId(SequenceTool.nextId() + "L");
            paramBundle.setCreateDate(DateUtil.getDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
            paramBundle.setParamType(EnumParamType.CODE);
            paramBundles.add(paramBundle);
        }
        //添加枚举信息
        for (EnumField enumField : enumLists) {
            ParamBundle paramBundle = new ParamBundle();
            paramBundle.setClassName(enumField.getEnumCode().replaceAll(enumFlag,""));
            paramBundle.setPackageName(enumField.getPackagePath());
            paramBundle.setEnumItemList(enumField.getItemList());
            String moduleName = paramBundle.getClassName()
                .substring(0, 1)
                .toLowerCase() + paramBundle.getClassName()
                .substring(1);
            paramBundle.setModuleName(moduleName);
            paramBundle.setModuleId(configuration.getModuleId());
            paramBundle.setModuleIdUp(configuration.getModuleId()
                .toUpperCase());
            paramBundle.setModuleIdCn(configuration.getModuleIdCn());
            paramBundle.setAuthor(configuration.getAuthor());
            paramBundle.setSerialId(SequenceTool.nextId() + "L");
            paramBundle.setCreateDate(DateUtil.getDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
            paramBundle.setParamType(EnumParamType.ENUM);
            paramBundles.add(paramBundle);
        }
        context.setParamBundles(paramBundles);

    }
}
